package com.webbei.Backstage.dao.impl;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.webbei.Backstage.dao.BackstateDao;
import com.webbei.Backstagepojo.Labelcost;
import com.webbei.pojo.Customer;
import com.webbei.pojo.Customerlimit;
import com.webbei.pojo.IndusConproduct;
import com.webbei.pojo.Industry;
import com.webbei.pojo.Managers;
import com.webbei.pojo.Ordercost;
import com.webbei.pojo.Orders;
import com.webbei.pojo.Products;
import com.webbei.pojo.Report;
import com.webbei.pojo.Userhot;

public class BackstateDaoImpl implements BackstateDao {
       private SqlSession session;
       
    /*  public SqlSession getSession() {
		return session;
	}*/

	public void setSession(SqlSession session) {
		this.session = session;
	}

	@Override
	public List<Report> relist(int start) {
		return session.selectList("report.getall",start);
	}

	@Override
	public List<Managers> malist() {
		return session.selectList("manager.getall");
	}

	@Override
	public List<Customer> cuslist() {
		return session.selectList("customer.getUsername");
	}

	@Override
	public Customer findcusbyid(int id) {
		return (Customer) session.selectOne("customer.getCustomer", id);
	}

	@Override
	public List<Customer> fenyelist(int start) {
		return session.selectList("customer.getcusfenye",start);
	}

	@Override
	public Customer findcusbyname(String uname) {
		 return (Customer) session.selectOne("customer.getCustomerByUserName", uname);
	}

	@Override
	public int getreortCount() {
		return (Integer) session.selectOne("report.getcount");
	}

	@Override
	public int reportsuccess(String username,int count) {
		Userhot hot=new Userhot();
		hot.setUserName(username);
		hot.setBereportedCount(count);
		return session.update("userhot.upbereportCount",hot);
	}

	@Override
	public int reportfail(String username,int count) {
		Userhot hot=new Userhot();
		hot.setUserName(username);
		hot.setReportfailCount(count);
		return session.update("userhot.upreportfailCount",hot);
	}

	@Override
	public Userhot uhot(String username) {
		//System.out.println("zheliuser:"+username);
		Userhot hot=(Userhot) session.selectOne("userhot.getUserhotByUsername",username);
		//System.out.println("hottt:"+hot);
	   return hot;
	}

	@Override
	public int customerlimitCount() {
	  return (Integer) session.selectOne("cuslimit.getlimitCount");
	}

	@Override
	public List<Customerlimit> cuslimitlist(int start) {
		  return session.selectList("cuslimit.getall",start);
	}

	@Override
	public int updatelimit(String name, int share, int ping, int ju, int ding) {
		Customerlimit limi=new Customerlimit();
		limi.setUserName(name);
		limi.setCanShare(share);
		limi.setCanCustom(ding);
		limi.setCanComment(ping);
		limi.setCanReport(ju);
		return session.update("cuslimit.updatelimit",limi);
	}

	@Override
	public Customerlimit cuslimi(String username) {
		return (Customerlimit) session.selectOne("cuslimit.getonelimi", username);
	}

	@Override
	public List<Report> getrecontent(String bename) {
		return session.selectList("report.getrecontent",bename);
	}

	@Override
	public List<Industry> getallinformation() {
	 return session.selectList("Mindustry.getinduinformation");
	}
	
   
	
       public int deleteindus(int id){
    	  return session.delete("Mindustry.deleteindus", id);
       }

	@Override
	public Industry getone(int id) {
		return (Industry) session.selectOne("Mindustry.getone",id);
	}

	
	public List<Orders> orderlist(int start) {
		return session.selectList("orders.getOrder",start);
	}
	
	public int getOrdercount(String datetime,String indusname){
		System.out.println("time=="+datetime+"indusname=="+indusname);
		
		Map map=new HashMap();
		map.put("indusname",indusname);
		map.put("datetime", datetime);
		 return (Integer) session.selectOne("orders.getorderCount1",map);
		/*int count = 0;
		if(!datetime.equals("kong")&&!indusname.equals("kong")){
			map.put("indusname",indusname);
			map.put("datetime", datetime);
	     	count=(Integer) session.selectOne("orders.getorderCount3",map);
		}
			if(!datetime.equals("kong")&&indusname.equals("kong"))
			{
				System.out.println("jinlale");
			 map.put("datetime", datetime);
			 count=(Integer) session.selectOne("orders.getorderCount1",map);
			}
		
		if(!indusname.equals("kong")&&datetime.equals("kong"))
	    {
			//System.out.println("jinlaile");
			map.put("indusname",indusname);
		     count=(Integer) session.selectOne("orders.getorderCount2",map);
			}
		
		return count;*/
	}

	@Override
	public Orders getorder(int orderid) {
		return (Orders) session.selectOne("orders.getOrdersById", orderid);
	}

	@Override
	public String getindusNamebyid(int id) {
	return  (String) session.selectOne("Mindustry.getindusNamebyid", id);
	}

	@Override
	public int updCDRstateAndinCdrpath(String path,int orderid) {
		Orders order=new Orders();
		order.setCDRurl(path);
		order.setOrderId(orderid);
		return session.update("orders.insertCDRpath",order);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Orders> getorderByday(String day,String indusname,int start) {
		Map map=new HashMap();
		List<Orders> list=new ArrayList<Orders>();
		map.put("day", day);
		map.put("indusname", indusname);
		map.put("start", start);
		 list=session.selectList("orders.getOrderbyday1",map);
		/*if(!day.equals("kong")&&!indusname.equals("kong")){
		map.put("day", day);
		map.put("indusname", indusname);
		map.put("start", start);
	    list=session.selectList("orders.getOrderbyday1",map);
		}
		if(!day.equals("kong")&&indusname.equals("kong")){
				map.put("day",day);
				map.put("start", start);
				 list=session.selectList("orders.getOrderbyday2",map);
			}
			if(!indusname.equals("kong")&&day.equals("kong")){
				map.put("indusname",indusname);
				map.put("start", start);
				 list=session.selectList("orders.getOrderbyday3",map);
			}
		*/
		return list;
	}

	@Override
	 public int getOrdercountByindusid(String datetime,int indusid,String uname,int orderstate) {
		Map map=new HashMap();
		map.put("datetime",datetime);
		map.put("indusid", indusid);
		map.put("uname",uname);
		map.put("orderstate",orderstate);
		return (Integer) session.selectOne("orders.getindusCount",map);
	}

	@Override
	public List<Orders> getindusorderByday(String day,int start,int indusid,String uname,int orderstate) {
		Map map=new HashMap();
		map.put("datetime", day);
		map.put("start", start);
		map.put("indusid", indusid);
		map.put("uname", uname);
		map.put("orderstate", orderstate);
		return session.selectList("orders.getindusOrder",map);
	}

	@Override
	public String getconmentcontentbyid(int id) {
		String str=(String)session.selectOne("Customcomment.getcommentContent",id);
		return str;
	}
    
	 public int deletecomment(int commentid){
		 return session.delete("Customcomment.deletecomment",commentid);
	 }

	@Override
	public int updateindus(int id, String industryName, String industryAddress,
			String industryPrincipal, String industryTel, String industryEmail) {
		Industry dus=new Industry();
		dus.setIndustryAddress(industryAddress);
		dus.setIndustryEmail(industryEmail);
		dus.setIndustryId(id);
		dus.setIndustryName(industryName);
		dus.setIndustryprincipal(industryPrincipal);
		dus.setIndustryTelephone(industryTel);
	  return session.update("Mindustry.updateinfomation",dus);
	}

	@Override
	public int addindus(String industryName, String industryAddress,
			String industryPrincipal, String industryTel, String industryEmail) {
         Industry ins=new Industry();
				 ins.setIndustryName(industryName);
				 ins.setIndustryprincipal(industryPrincipal);
				 ins.setIndustryAddress(industryAddress);
				 ins.setIndustryEmail(industryEmail);
				 ins.setIndustryTelephone(industryTel);
				 return session.insert("Mindustry.insertindus",ins);
	}

	@Override
	public String getproductNamebyid(int proid) {
	 String str=(String)session.selectOne("Products.getpronamebyID",proid);
	 return str;
	}


	public List<Products> gotoAddindus() {
	return session.selectList("Products.getProducts");
	}

	
	public int updatecommentIdInreport(int reportid) {
		Report re=new Report();
		re.setReportId(reportid);
		re.setCommentId(-1);
		return session.update("report.updatecommentid", re);
	}

	@Override
	public List<String> proName(int indusid) {
		Map map=new HashMap();
		map.put("indusid",indusid);
		return session.selectList("indusconpro.getprobyindusid",map);
	}
	
	 public int insertinconpro(int indusid,int proid){
		 IndusConproduct dus=new IndusConproduct();
		 dus.setIndustryId(indusid);
		 dus.setProductId(proid);
		 return session.insert("indusconpro.insertproid",dus);
	 }
	 
	 public int getindusIdbyName(String name){
		 return (Integer) session.selectOne("Mindustry.getindusidbyName", name);
	 }

	@Override
	public int deleteproidByindusid(int indusid) {
		 return session.delete("indusconpro.deleteprobyindusid",indusid);
	}

	@Override
	public List<String> moren(int indusid) {
		Map map=new HashMap();
		map.put("indusid",indusid);
		return session.selectList("indusconpro.getmorenProduct",map);
	}

	 public int updatexuanzhong(int indusid,String proname){
		Map map=new HashMap();
		map.put("indusid", indusid);
		map.put("proname", proname);
		
		for(Object o:map.keySet()){
		  System.out.println("aaaaaa:"+map.get(o));
		}
		 return session.update("indusconpro.updatexuanzhong",map);
	 }
	    public int updateExceptxuanzhong(int indus,String proname){
	    	Map map=new HashMap();
			map.put("indusid", indus);
			map.put("proname", proname);
			 return session.update("indusconpro.updateExceptxuan",map);
	    }

		@Override
		public List getCostnameandCostmoney(int backpicid) {
			//System.out.println("jinlaile");
			//Map map=new HashMap();
			List<Labelcost> result=new ArrayList<Labelcost>();
			List list=new ArrayList();
			list=session.selectList("orders.getCostNameandCostmoney",backpicid);
			//System.out.println("chulaile");
			/* Collection c = map.values();
		        Iterator it = c.iterator();
		        for (; it.hasNext();) {
		            System.out.println(it.next());
		        }*/
			for(int i=0;i<list.size();i++){
				Map map=(Map) list.get(i);
				
				//System.out.println(map);
				/*Iterator iter = map.entrySet().iterator(); 
				while (iter.hasNext()) {    
				Map.Entry entry = (Map.Entry) iter.next();     
				Object key = entry.getKey();    
				Object val = entry.getValue();*/
				Labelcost l=new Labelcost();
				l.setLabelcostmoney((Double)map.get("labelcostMoney"));
				l.setLabelcostname((String)map.get("labelcostName"));
				result.add(l);
			}
			
			
		
		    return result;
		}

		@Override
		public int updateOrderstate(int orderid) {
			return session.update("orders.updateOrderstate",orderid);
		}

		@Override
		public int updateStockout(int orderid, String liyou) {
			Orders or=new Orders();
			or.setOrderId(orderid);
			or.setStockoutReason(liyou);
			return session.update("orders.stockoutreason",or);
			
		}

		@Override
		public int updateFeedbackreson(int orderid, String reason) {
			Orders or=new Orders();
			or.setOrderId(orderid);
			or.setCostFeedback(reason);
			
			return session.update("orders.updateFeedback", or);
		}

		@Override
		public int insertcost(Ordercost orcost) {
			return session.insert("ordermoneycost.insertmoneycost", orcost);
		}

		@Override
		public List<Orders> orderlist(int orderid,String ordername,int indusid, String time1, String time2,int start) {
			Map map=new HashMap();
			map.put("t1", time1);
			
			if(orderid==-1)
				map.put("orid",-1);
			else
				map.put("orid",orderid);
			
			if(time1!=""&&time1!=null){
			if(time2.equals("")||time2==null){
				    Date t=new Date();
			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			        String dateString = formatter.format(t);
			        //System.out.println("zhelidedate:"+dateString);
			        map.put("t2",dateString);
			}
			else
			{
			map.put("t2", time2);
			}
			}
			map.put("indusid", indusid);
			map.put("orname",ordername);
			map.put("start", start);
			return session.selectList("orders.getorderForCost",map);
		}
		
		 public List<Ordercost> ordercost(){
			 return session.selectList("ordermoneycost.getall");
		 }
     
		 public Ordercost getoneordercost(int orderid){
			 return (Ordercost) session.selectOne("ordermoneycost.getone",orderid);
		 }

		@Override
		public List<Orders> allindusorderlist(int orderid,String ordername,int indusid, String time1,
				String time2,int start) {
			Map map=new HashMap();
			map.put("t1", time1);
			
			if(time1!=""&&time1!=null){
			if(time2.equals("")||time2==null){
				    Date t=new Date();
			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			        String dateString = formatter.format(t);
			        System.out.println("zhelidedate:"+dateString);
			        map.put("t2",dateString);
			}
			else
			{
			map.put("t2", time2);
			}
			}
			if(orderid==-1){
				map.put("orid",-1);
			}
			else{
				map.put("orid",orderid);
			}
			
			if(indusid==-1)
			{
				System.out.println("woshi:"+indusid);
				map.put("indusid",-1);
			}
			else
			map.put("indusid",indusid);	
			
			map.put("orname",ordername);
			map.put("start", start);
			return session.selectList("orders.getallindusorderForCost", map);
		}

		@Override
		public String get9dingid(int orderid) {
			String dd=orderid+"";
			String str="";
			if(dd.length()<9){
				 DecimalFormat df=new DecimalFormat("000000000");     
				  str=df.format(orderid);
			}
			else{
				str=dd;
			}
			return str;
		}

		@Override
		public String chaifendingid(String dingid) {
			 return dingid.replaceAll("^(0+)", "");
         }

		@Override
		public int getoneOrdercostCount(int orerid, String ordername,
				int indusid, String time1, String time2) {
			Map map=new HashMap();
			map.put("t1", time1);
			
			if(orerid==-1)
				map.put("orid",-1);
			else
				map.put("orid",orerid);
			
			if(time1!=""&&time1!=null){
			if(time2.equals("")||time2==null){
				    Date t=new Date();
			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			        String dateString = formatter.format(t);
			        map.put("t2",dateString);
			}
			else
			{
			map.put("t2", time2);
			}
			}
			map.put("indusid", indusid);
			map.put("orname",ordername);
			return (Integer) session.selectOne("orders.CountorderForCost", map);
		}

		@Override
		public int allOrdercostCount(int orderid, String ordername,
				int indusid, String time1, String time2) {
			Map map=new HashMap();
			map.put("t1", time1);
			
			if(time1!=""&&time1!=null){
			if(time2.equals("")||time2==null){
				    Date t=new Date();
			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			        String dateString = formatter.format(t);
			        System.out.println("zhelidedate:"+dateString);
			        map.put("t2",dateString);
			}
			else
			{
			map.put("t2", time2);
			}
			}
			if(orderid==-1){
				map.put("orid",-1);
			}
			else{
				map.put("orid",orderid);
			}
			
			if(indusid==-1)
			{
				System.out.println("woshi:"+indusid);
				map.put("indusid",-1);
			}
			else
			map.put("indusid",indusid);	
			
			map.put("orname",ordername);
			
			return (Integer) session.selectOne("orders.CountallindusorderForCost", map);
		}

		
		public String getCdrurlByorderid(int orderid) {
			return (String) session.selectOne("orders.getcdrurl",orderid);
		}

}
